
/**
 *  Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.dao;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.Query;
import javax.persistence.EntityManager;

import ca.ioniq.shared.entity.Order;

public class OrderDAO extends GenericDAO<Order> {

	@Inject
	public OrderDAO(EntityManager em) {
		super(em, Order.class);
	}
	
	public OrderDAO(Class<Order> persistentClass) {
		super(persistentClass);
	}

	@SuppressWarnings("unchecked")
	public List<Order> findOrdersAfterAGivenDate(Date date) {
		Query query = em.createQuery("from Order o where o.datePlaced >= :date");
		query.setParameter("date", date);
		return query.getResultList();		
	}
	
    // discriminate on companyId
	@SuppressWarnings("unchecked")
	public List<Order> findOrdersAfterAGivenDateAndCompany(Date date, Long companyId) {
		Query query = em.createQuery("from Order o where o.datePlaced >= :date and o.company.id = :companyId");
		query.setParameter("date", date);
		query.setParameter("companyId", companyId);
		return query.getResultList();		
	}

	@SuppressWarnings("unchecked")
	public List<Order> findAll() {
		Query query = em.createQuery("from Order");
		return query.getResultList();		
	}

    //discriminate by companyId
	@SuppressWarnings("unchecked")
	public List<Order> findAllByCompany(Long companyId) {
		Query query = em.createQuery("from Order o where o.company.id = :companyId");
		query.setParameter("companyId", companyId);
		return query.getResultList();		
	}
	
	@SuppressWarnings("unchecked")
	public List<Order> viewOrdersByUser(Long userId) {
		Query query = em.createQuery("from Order o where o.user.id = :userId");
		query.setParameter("userId", userId);
		return query.getResultList();	
	}

    //discriminate by companyId
	@SuppressWarnings("unchecked")
	public List<Order> viewOrdersByUserAndCompany(Long userId, Long companyId) {
		Query query = em.createQuery("from Order o where o.user.id = :userId and o.company.id = :companyId" );
		query.setParameter("userId", userId);
		query.setParameter("companyId", companyId);
		return query.getResultList();	
	}
	
}
